package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.entities.ValuePhoneUserEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.GlideUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/1/21.
 */
public class PhoneUserDetailsActivity extends BaseSwipeBackActivity {

    private static final String TAG = PhoneUserDetailsActivity.class.getSimpleName();
    private static final int HIDEPROGRESS = 1000;
    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.weikai)
    TextView weikai;
    @BindView(R.id.yaoqing)
    Button yaoqing;
    @BindView(R.id.pre_v_back)
    ImageView pre_v_back;

    private String namestr;
    private String isHave;
    private String numberstr;
    private String head_im;
    private Bitmap bitmap;
    private PGService mPGservice;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };

    public static void startActivity(Activity activity, String name, String number, String isHave, String head){
        Intent intent = new Intent(activity,PhoneUserDetailsActivity.class);
        intent.putExtra("name",name);
        intent.putExtra("number",number);
        intent.putExtra("isHave",isHave);
        intent.putExtra("head",head);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.phone_user_yaoqing);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        mPGservice = PGService.getInstance();

        namestr = getIntent().getStringExtra("name");
        numberstr = getIntent().getStringExtra("number");
        isHave = getIntent().getStringExtra("isHave");
        head_im = getIntent().getStringExtra("head");
//        byte[] bis = getIntent().getByteArrayExtra("bitmap");
//        bitmap = BitmapFactory.decodeByteArray(bis, 0, bis.length);

//        importPhone(numberstr);

        name.setText(namestr);
        if ("1".equals(isHave)) {//not_yet_dredge_y
            weikai.setText(namestr + getString(R.string.not_yet_dredge_y));
            yaoqing.setVisibility(View.GONE);
        } else {
            weikai.setText(namestr + getString(R.string.not_yet_dredge));
            yaoqing.setVisibility(View.VISIBLE);
        }

        number.setText(numberstr);
        if (null != bitmap) head.setImageBitmap(bitmap);

        GlideUtils.loadCircleImage(PhoneUserDetailsActivity.this,head_im,head);

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_phone_user_details;
    }

    @OnClick({R.id.yaoqing})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yaoqing:
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + numberstr));
                intent.putExtra("sms_body", getResources().getString(R.string.yaoqing_send_message));
                startActivity(intent);
                doBind();
                break;
            case R.id.pre_v_back:
                scrollToFinishActivity();
                break;
        }
    }

    /**
     * 用户与被推荐人的绑定
     */
    private void doBind() {
        String userId = ToolsUtils.getPhpUid();
        mPGservice.setrefer(userId, numberstr).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                Log.e(TAG, "onResultError:  绑定推荐人失败");
            }

            @Override
            public void onNext(ValidateEntivity validateEntivity) {
                showToast(getResources().getString(R.string.bang_tuijian_success));
            }
        });
    }

    /**
     * 检测手机联系人是否已经注册过了
     */
//    private void importPhone(String phones) {
//        if (!ToolsUtils.currentNetState(this)) {
//            return;
//        }
//        showProgress(null);
//        Log.e("-----", "importPhone: phones=这个要判断，到底走了几次---" + phones);
//        mPGservice.importPhone(phones, ToolsUtils.getPhpUid()).subscribe(new AbsAPICallback<String>() {
//            @Override
//            protected void onResultError(ApiException ex) {
//                handler.sendEmptyMessage(HIDEPROGRESS);
//                hideProgress();
////                showToast("验证提交失败!");
//                Log.e(TAG, "onResultError: 检测手机号是否已经注册过---失败");
//            }
//
//            @Override
//            public void onNext(String validateEntivity) {
//                hideProgress();
//                handler.sendEmptyMessage(HIDEPROGRESS);
////                Log.e(TAG, "onResultError: 检测手机号是否已经注册过---成功----" + validateEntivity.getInfo().get(0).getIsHave()+"----" +validateEntivity.getInfo().get(0).getPhone());
//            }
//        });
//    }
}
